package ar.com.angrymachine.hunter.gameplay;

import java.awt.Image;

import ar.com.angrymachine.hunter.Game;

public class Trap implements Cloneable, Leveled
{
    public int timeToFire;
    public int timeToDisarm;
    public int disarmTime;
    public int damage;
    public int fireTime;
    public String name;
    public Image icon;
    public Treasure treasure = null;
    private int level;
    private boolean fired = false;
    private static final int STATE_IDLE = 0;
    private static final int STATE_RUNNING = 1;
    private int state = STATE_IDLE;
    
    public Trap(String name, Image icon, int damage, int level)
    {
        this.name = name;
        this.damage = damage;
        this.icon = icon;
        this.level = level;
        timeToFire = Math.max(500, 5000 - level * 500);
        timeToDisarm = level * 2000;
        disarmTime = 0;
        fireTime = 0;
    }
    
    public boolean isFired()
    {
        return fired;
    }
    
    public boolean isDisarmed()
    {
        return disarmTime >= timeToDisarm;
    }
    
    public boolean isIdle()
    {
    	return state == STATE_IDLE;
    }
    
    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            return null;
        }
    }
        
    public void tick(int delta)
    {
        if(!isFired() && !isDisarmed() && state == STATE_RUNNING)
        {
            fireTime += delta;
            if (fireTime >= timeToFire)
            {
            	fireTime = timeToFire;
                fire();
            }
        }
    }
    
    public void wake()
    {
    	if(state == STATE_IDLE)
    	{
    		state = STATE_RUNNING;
    	}
    }

    public void fire()
    {
        fired = true;
        Game.current.player.receiveAttack(damage);
    }

    public int getLevel()
    {
        return level;
    }
}
